/*
 * FileName: 22Yards.cs
 * Author: Gourav Das
 * Created: 08/09
 * Copyright: Gourav Das, 2011
 */


using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;
using _22yards._2D;
using _22yards.Scenes;
using _22yards.Scenes.Helpers;
using _22yards.Helpers;
using _22yards.Core;
using _22yards.EnumDeclarations;
using _22yards.ThreeD;

namespace _22yards
{
    /// <summary>
    /// Scaling factor
    /// </summary>
    public class ScalingFactor
    {
        private float width = 1366;
        private float height = 768;

        private float heighttScale;
        private float WidthScale;

        /// <summary>
        /// Gets the height scaling factor.
        /// </summary>
        public float htScale
        {
            get
            {
                return heighttScale;
            }
        }

        /// <summary>
        /// Gets the width scaling factor.
        /// </summary>
        public float widthScale
        {
            get
            {
                return WidthScale;
            }
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="ScalingFactor"/> class.
        /// </summary>
        /// <param name="_Height">Height of the _.</param>
        /// <param name="_Width">Width of the _.</param>
        public ScalingFactor(int _Height, int _Width)
        {
            heighttScale = _Height / height;
            WidthScale = _Width / width;
        }

    }


    /// <summary>
    /// This is the main type for game
    /// </summary>
    public class Game22Yards : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics;
        TextNFontEngine txtNfont;
        SceneEngine sceneEngine;
        FrameRateCounter frameRateCtr;
        MouseCordinates mouseCordinates;
        //SpriteBatch spriteBatch;
        
        /// <summary>
        /// ProgressBar
        /// </summary>
        public static ProgressBar pb1;

        /// <summary>
        /// Instance of ScalingFactor to be used accross all screens
        /// </summary>
        public static ScalingFactor scaleFactor;

        /// <summary>
        /// Static instance of Match to be used by all screens if 1 off match is being played
        /// </summary>
        public static Match match;
        /// <summary>
        /// Static instance of Match Engine to be used by all screen
        /// </summary>
        public static MatchPlayEngine matchPlayEngine;

        /// <summary>
        /// Static instance of Dyn Message store to be used by all screens
        /// </summary>
        public static DynamicMessageStore DynMsgStore=new DynamicMessageStore();

        // By preloading any assets used by UI rendering, we avoid framerate glitches
        // when they suddenly need to be loaded in the middle of a menu transition.
        static readonly string[] preloadAssets =
        {
            "TwoDee/Screens/MsgBox",
        };

        /// <summary>
        /// Initializes a new instance of the <see cref="Game22Yards"/> class.
        /// </summary>
        public Game22Yards()
        {
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "22yardsContent";
            graphics.PreferMultiSampling = true;
            graphics.PreferredBackBufferWidth = 1366;// GraphicsAdapter.DefaultAdapter.CurrentDisplayMode.Width;
            graphics.PreferredBackBufferHeight = 768;// GraphicsAdapter.DefaultAdapter.CurrentDisplayMode.Height;
            graphics.IsFullScreen = !true;
            this.IsMouseVisible = true;
            //graphics.ToggleFullScreen();
            scaleFactor = new ScalingFactor(graphics.PreferredBackBufferHeight, graphics.PreferredBackBufferWidth);
            this.IsFixedTimeStep = true;
            graphics.SynchronizeWithVerticalRetrace = true;
            
        }

        /// <summary>
        /// Allows the game to perform any initialization it needs to before starting to run.
        /// This is where it can query for any required services and load any non-graphic
        /// related content.  Calling base.Initialize will enumerate through any components
        /// and initialize them as well.
        /// </summary>
        protected override void Initialize()
        {
            // TODO: Add your initialization logic here
            txtNfont = new TextNFontEngine(this);
            sceneEngine = new SceneEngine(this, txtNfont);
            frameRateCtr = new FrameRateCounter(this);
            mouseCordinates = new MouseCordinates(this);
            //sceneEngine.TraceEnabled = true;
            pb1 = new ProgressBar(this, "Progress Bar1");

            Components.Add(sceneEngine);
            //Components.Add(txtNfont);
            Components.Add(frameRateCtr);
            Components.Add(mouseCordinates);
            //Components.Add(pb1);

            //Brings Xbox live login screen on pressing Homekey
            Components.Add(new GamerServicesComponent(this));


            sceneEngine.AddScene(new BackgroundScene(), null);
            sceneEngine.AddScene(new MainMenuScene(), null);
            
            
            match = new Match();
            matchPlayEngine = new MatchPlayEngine();
            
            base.Initialize();
        }

        /// <summary>
        /// LoadContent will be called once per game and is the place to load
        /// all of your content.
        /// </summary>
        protected override void LoadContent()
        {
            // Create a new SpriteBatch, which can be used to draw textures.
            //spriteBatch = new SpriteBatch(GraphicsDevice);

            // TODO: use this.Content to load your game content here
            foreach (string asset in preloadAssets)
            {
                Content.Load<object>(asset);
            }
        }

        /// <summary>
        /// UnloadContent will be called once per game and is the place to unload
        /// all content.
        /// </summary>
        protected override void UnloadContent()
        {
            // TODO: Unload any non ContentManager content here
            Content.Unload();
        }

        /// <summary>
        /// Releases all resources used by the Game class.
        /// </summary>
        /// <param name="disposing">true to release both managed and unmanaged resources; false to release only unmanaged resources.</param>
        protected override void Dispose(bool disposing)
        {
            base.Dispose(disposing);
        }

        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Update(GameTime gameTime)
        {
            // Allows the game to exit
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
                this.Exit();

            // TODO: Add your update logic here
            base.Update(gameTime);
        }

        /// <summary>
        /// This is called when the game should draw itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Draw(GameTime gameTime)
        {
          // TODO: Add your drawing code here

            base.Draw(gameTime);
        }
    }
}
